package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.Part;
import com.zmj.sy.mom.srv.aps.bean.vo.part.PartInfoResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.work.PoolItemVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PartMapper extends SyMapper<Part> {

    @Delete("delete from tb_mes_part where id = #{id}")
    void deleteReal(Integer id);

    @Select("SELECT mpp.plm_id,mpp.construct_no cuconstructNo,mpp.loading_code palletCode ,mpp.part_count palletCount,IFNULL(mp.pre_phase_code,mpp.pre_phase_code) nxphase,mp.part_count totalCount ,iFNULL( mp.work_group,'2-4_KJGW_00')  nextGroupNumber, IFNULL(mp.phase_code,mpp.phase_code) nextNxphase,  op.png_url partPath,op.texture,IFNULL( op.length,- 1 ) partLength,IFNULL( op.width,- 1 ) partWidth,IFNULL( op.thickness,- 1 ) partHeight,IFNULL( op.net_weight,- 1 ) partWeight\n" +
            " from tb_mes_part_pallet mpp LEFT JOIN tb_mes_part mp on mpp.plm_id=mp.plm_id and mpp.phase_code=mp.phase_code and mpp.construct_no=mp.construct_no \n" +
            " LEFT JOIN tb_order_part  op on  mpp.plm_id=op.plm_id\n" +
            " where mpp.loading_code=#{palletCode} and mpp.type=1")
    List<PartInfoResVo> selectPartInfoResVo(String palletCode);

    @Update("UPDATE tb_mes_part SET part_count =part_count + #{reportCount},fail_count=fail_count + #{failCount} WHERE id = #{id}")
    void updateCount(@Param("id") Integer id, @Param("reportCount") Integer reportCount, @Param("failCount") Integer failCount);

    @Update("UPDATE tb_mes_part SET part_count =part_count - #{reportCount} WHERE id = #{id}")
    void consumePart(@Param("id") Integer id, @Param("reportCount") Integer reportCount);

    @Delete("delete from tb_mes_part where plm_id = #{plmId}")
    void deletePlate(String plmId);

    @Select(" select pi.seq seq, pi.pool_id poolId, pi.construct_code constructNo " +
            " from tb_mes_pool_item pi " +
            " where pi.pool_id in ( " +
            "   select t.pool_id from tb_mes_pool_item t where t.construct_code in ('${constructList}') and t.status = 1 and t.deleted = 0 " +
            " ) and pi.status = 1 and pi.deleted = 0 ")
    List<PoolItemVo> selectPoolItem(@Param("constructList") String constructList);

    @Delete("DELETE FROM tb_mes_part p ${ew.customSqlSegment} ")
    void deletePartZero(@Param(Constants.WRAPPER) QueryWrapper<Part> qw);

    @Select("SELECT DISTINCT p.id FROM tb_mes_part p LEFT JOIN tb_order o ON p.construct_no = o.construct_no ${ew.customSqlSegment} ")
    List<Integer> selectPartZero(@Param(Constants.WRAPPER) QueryWrapper<Part> qw);
}
